<?php
/**
 * Database handler abstract class for the KvFramework
 * @package KvFramework_v2
 * @subpackage KvF_DbHandler
 * @author Greg McWhirter <gsmcwhirter@gmail.com>
 * @copyright Copyright &copy; 2006, Greg McWhirter
 * @license MIT License
 * @version $Revision$
 */

load_files(
  KVF_FRAMEWORK."/kvf_dbhandler/kvf_db_query.class.php",
  KVF_FRAMEWORK."/kvf_dbhandler/kvf_db_object.class.php",
	KVF_FRAMEWORK."/kvf_dbhandler/structs/kvf_db_struct_query.struct.php",
	KVF_FRAMEWORK."/kvf_dbhandler/structs/kvf_db_struct_query_delete.struct.php",
	KVF_FRAMEWORK."/kvf_dbhandler/structs/kvf_db_struct_query_find.struct.php",
	KVF_FRAMEWORK."/kvf_dbhandler/structs/kvf_db_struct_query_insert.struct.php",
	KVF_FRAMEWORK."/kvf_dbhandler/structs/kvf_db_struct_query_update.struct.php",
	KVF_FRAMEWORK."/kvf_dbhandler/exceptions/kvf_dbhandler.exception.php",
	KVF_FRAMEWORK."/kvf_dbhandler/dbhandlers/kvf_dbhandler_mysql.class.php"
);

/**
 * Abstract database handler class
 *
 * @package KvFramework_v2
 * @subpackage KvF_DbHandler
 */
abstract class KvfDbhandler
{
  /**
   * Constructor.  Logs initialization.
   *
   */
  function __construct()
  {
    //TODO: add logging
  }

  /**
   * Execute an SQL query
   *
   * @param string $sql The query to execute
   * @param boolean $resultset Whether to return a resultset or not
   * @param array $params Parameters for the query
   */
  abstract public function Query($sql, array $params = array());
  /**
   * Fetch a database record from a query as an object
   *
   * @param KvfDbQuery $qid
   */
  abstract public function Fetch(KvfDbQuery &$qid);
  /**
   * Fetch a database record from a query as an array
   *
   * @param KvfDbQuery $qid
   */
  abstract public function FetchArray(KvfDbQuery &$qid);
  /**
   * Get the number of rows returned for a query
   *
   * @param KvfDbQuery $qid
   */
  abstract public function Rows(KvfDbQuery $qid);
  /**
   * Get the error from a query
   *
   * @param string $ret
   */
  abstract public function Error($ret);
  /**
   * Return the number of rows affected by the last query
   *
   */
  abstract public function Affected();
  /**
   * Return the last inserted id
   *
   */
  abstract public function Inserted();
  /**
   * Escape the parameter so it is safe to insert into a query
   *
   * @param mixed $val
   */
  abstract public function Escape($val);
  /**
   * Process a query struct according to its type.
   *
   * @param KvfDbStructQuery $query
   */
  abstract public function Process(KvfDbStructQuery $query);
  
  /**
   * Generate a query struct for a find query
   *
   * @param mixed $tables
   * @return KvfDbStructQueryFind
   */
  public function FindQuery($tables)
  {
    if(!is_array($tables))
    {
      $tables = array($tables);
    }
    return new KvfDbStructQueryFind($tables);
  }

  /**
   * Generate a query struct for a delete query
   *
   * @param mixed $tables
   * @return KvfDbStructQueryDelete
   */
  public function DeleteQuery($tables)
  {
    if(!is_array($tables))
    {
      $tables = array($tables);
    }
    return new KvfDbStructQueryDelete($tables);
  }

  /**
   * Generate a query struct for an insert query
   *
   * @param mixed $tables
   * @return KvfDbStructQueryInsert
   */
  public function InsertQuery($tables)
  {
    if(!is_array($tables))
    {
      $tables = array($tables);
    }
    return new KvfDbStructQueryInsert($tables);
  }

  /**
   * Generate a query struct for an update query
   *
   * @param mixed $tables
   * @return KvfDbStructQueryUpdate
   */
  public function UpdateQuery($tables)
  {
    if(!is_array($tables))
    {
      $tables = array($tables);
    }
    return new KvfDbStructQueryUpdate($tables);
  }
}

?>
